package com.ts.boot.service;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import com.ts.boot.common.CityCodeUtil;
import org.springframework.stereotype.Service;
import us.codecraft.webmagic.Request;

import java.util.LinkedList;

@Service
public class Server {

    private LinkedList<Request> urlList = new LinkedList<>();

    public LinkedList<Request> getUrlList(String dCity,String aCity,String startDate,String endDate){

        String nextDate = startDate;

        while(!nextDate.equals(endDate)){
            String url = "https://flights.ctrip.com/online/list/oneway-" + CityCodeUtil.getCityCode(dCity) +
                    "-" + CityCodeUtil.getCityCode(aCity) + "?_=1&depdate=" + nextDate;
            urlList.add(new Request(url));
            nextDate = getDateNext(nextDate);
        }

        return urlList;
    }

    public String getDateNext(String date){

        return DateUtil.offset(DateUtil.parseDate(date), DateField.DAY_OF_MONTH,1).toString().substring(0,10);
    }


}
